<?php

namespace app\admin\controller;

use think\Validate;
use think\Db;
use app\models\{Message, Annous, Annouslog, User as Users, Address, Banner, Porder as Orders, PorderGoods, Region, PlatClass, Config, PlatSons, Withdraw, Agent, Business, BusinessArea, Pgoods, PgoodsSpec, SystemGoodsSpec, SystemGoods, ConvenClass, Conven, Rebot};

class System extends Base
{



    public function annous_message()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require',
        ], [
            'id.require' => 'ID不能为空',
        ]);
        if (!$validate->check($data)) {
            return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
        }
        Db::startTrans();
        try {
            (new Message())->allowfield(true)->save([
                'type' => 1,
                'orderid' => 0,
                'user_id' =>  $data['id'],
                'ouser_id' => 0,
                'status' => 0,
                'endstatus' => 0,
                'text' => '您有未阅读的公告|学习内容，请前往查看!',
                'yue' => 2
            ]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 1, 'data' => [], 'msg' => $e->getMessage()]);
        }
        return json(['status' => 0, 'data' => [], 'msg' => '发送成功']);
    }

    public function annous_user()
    {
        $id = $this->request->get('id/d', 0);
        $annous = (new Annous())->where('id', $id)->find();
        $types = explode(',', $annous['type']) ?? [];
        $list = (new Users())->order('id desc');
        $data = $this->request->get();
        $typearr = config('config.typepeople');
        if (!empty($data['title'])) {
            $list = $list->where('account|remark', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['type'])) {
            $list = $list->where('type', '=', $data['type']);
        }
        if (empty($annous)) {
            $list = $list->where('id', 0);
        }
        $list = $list->where('del', 1)->where(['type' => $types]);
        $list = $list->paginate(15, false, ['query' => $this->request->param()]);
        foreach ($list as $result) {
            $result['readid'] = $id;
            $result['type_text'] = $typearr[$result['type']] ?? '无';
            $result['read'] = 1; //1表示未读  2表示已读  3表示无权限
            if ($result['type'] == 2 && $annous['sonstype'] > 0 && $result['sonstype'] != $annous['sonstype']) {
                $result['read'] = 3;
            } else {
                $annouslog = (new Annouslog())->where(['user_id' => $result['id'], 'annousid' => $id])->count();
                if ($annouslog > 0) {
                    $result['read'] = 2;
                }
            }
        }
        return view(null, [
            'list' => $list,
            'typepeople' => config('config.typepeople')
        ]);
    }

    public function annous()
    {
        $list = (new Annous());
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['leixin'])) {
            $list = $list->where('leixin', $data['leixin']);
        }
        if (!empty($data['fltype'])) {
            $list = $list->where('fltype', $data['fltype']);
        }
        if (!empty($data['type'])) {
            $list = $list->where("FIND_IN_SET (" . $data['type'] . ",type)");
        }
        $sort = ['dates' => 'desc'];
        if (!empty($data['sort'])) {
            $sort = ['id' => 'desc'];
        }
        $typearr = config('config.typepeople');
        $list = $list->order($sort)->paginate(10, false, ['query' => $this->request->param()])->each(function ($row) use ($typearr) {
            $types = explode(',', $row['type']);
            $text = '';
            foreach ($types as $type) {
                $text = $text . ' ' . $typearr[$type] ?? '';
            }
            $row['type_text'] = $text;
            return $row;
        });
        $typelist = config()["config"]['typepeople'];
        return view(null, [
            'typelist' => $typelist,
            'list' => $list
        ]);
    }

    public function annous_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'type' => 'require'
            ], [
                'title.require' => '标题不能为空',
                'type.require' => '分组不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['type'] = implode(',', $data['type']);
            if (!empty($data['fujian'])) {
                $data['fujian'] = json_encode($data['fujian'], true);
            }
            if (!empty($data['id'])) {
                $res = (new Annous())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Annous())->allowfield(true)->save($data);
            }
            if ($res) {
                if (!empty($data['isread']) && !empty($data['id'])) {
                    (new Annouslog())->where('annousid', $data['id'])->delete();
                    return json(['status' => 0, 'msg' => '编辑并重置通知']);
                }
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Annous())->where('id', $id)->find();
            if ($row) {
                $row['type'] = explode(',', $row['type']);
                $row['fujian'] = json_decode($row['fujian'], true);
            }
            $typelist = config()["config"]['typepeople'];
            return view('', [
                'typelist' => $typelist,
                'row' => $row
            ]);
        }
    }

    public function annous_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除内容']);
            }
            $banner = (new Annous())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '内容不存在，请重试']);
            }
            $res = (new Annous())->where('id', $id)->delete();
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function annous_reset()
    {
        $id = $this->request->post('id/d', 0);
        if (empty($id)) {
            return json(['status' => 1, 'msg' => '请选择删除内容']);
        }
        $res = (new Annouslog())->where('annousid', $id)->delete();
        if ($res) {
            return json(['status' => 0, 'msg' => '操作成功']);
        }
        return json(['status' => 1, 'msg' => '操作失败']);
    }

    //正常订单
    public function order()
    {
        $results = (new Orders())->order(['id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn|id', $data['ordersn']);
        }
        if (!empty($data['status'])) {
            $results = $results->where('status', $data['status']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['status_name'] = $result->status_text;
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new PorderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        return view(null, [
            'list' => $results
        ]);
    }


    public function order_str()
    {
        $data = $this->request->post();
        if (empty($data['id']) || empty($data['status'])) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '信息参数错误!']);
        }
        $order = (new Orders())->where('id', $data['id'])->find();
        if (empty($order)) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '订单不存在!']);
        }
        if ($order['status'] == 4 || $order['status'] == 5) {
            return json(['status' => 1, 'code' => 1, 'msg' => '订单已完成或取消，无法继续操作!']);
        }
        switch ($data['status']) {
            case '2':
                break;
            case '3':
                if ($order['status'] != 2 && $order['status'] != 3) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可配送!']);
                }
                if (empty($data['value']) || empty($data['value2'])) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '物流信息不能为空!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 3, 'fwtimes' => date('Y-m-d H:i:s'), 'kname' => $data['value'], 'knumber' => $data['value2']]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已配送中!']);
                }
                break;
            //完成订单
            case '4':
                if ($order['status'] != 2 && $order['status'] != 3) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可完成!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 4, 'finishtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已完成!']);
                }
                break;
            //取消订单
            case '5':
                //未支付订单直接取消
                Db::startTrans();
                try {
                    $res = (new Orders())->where('id', $data['id'])->update(['status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                    if ($order['status'] == 2 || $order['status'] == 3) {
                        (new Users())->MoneyEdit($order['user_id'], 2, $order['total'], '取消订单积分返回', $order['ordersn']);
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                }
                if ($res) {
                    return json(['status' => 0, 'code' => 0, 'msg' => '订单已取消!']);
                }
                return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
            default:
                return json(['status' => 1, 'code' => 1, 'msg' => '操作状态出错!']);
                break;
        }
    }

    public function orderdetail()
    {
        $ordersn = $this->request->get('ordersn/d', 0);
        $list = (new PorderGoods())->where(['ordersn' => $ordersn])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
        }
        return view(null, [
            'list' => $list
        ]);
    }

    public function goodslist()
    {
        $model = new SystemGoods();
        $data = $this->request->get();
        $results = $model->where('isdel', 1)->order('id desc');
        $ptsons = [];
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['platclassid'])) {
            $results = $results->where('classid', $data['platclassid']);
        }
        if (!empty($data['isSale'])) {
            $results = $results->where('isSale', $data['isSale']);
        }
        if (!empty($data['isnew'])) {
            $results = $results->where('isnew', $data['isnew']);
        }
        if (!empty($data['isSale'])) {
            $results = $results->where('recomm', $data['recomm']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        $platclass = (new PlatClass())->order(['sort' => 'desc', 'id' => 'desc'])->select();
        foreach ($results as $result) {
            $result['images'] = json_decode($result['images'], true) ?? [];
            $result['class_name'] = (new PlatClass())->where('id', $result['classid'])->value('name');
            $result['goodspec'] = (new SystemGoodsSpec())->where(['isdel' => 1, 'goodid' => $result['id']])->order('is_default desc')->find();
        }
        return view(null, [
            'list' => $results,
            'platclass' => $platclass
        ]);
    }

    public function goodslist_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'descs' => 'require',
                'images' => 'require',
                'classid' => 'require',
                'spec' => 'require'
            ], [
                'title.require' => '服务名称不能为空',
                'descs.require' => '服务描述不能为空',
                'images.require' => '图片不能为空',
                'classid.require' => '平台大类不能为空',
                'spec.require' => '规格必须填写'
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (empty($data['spec']) || count($data['spec']) <= 0) {
                return json(['code' => 1, 'data' => [], 'msg' => '规格信息不能少于一个']);
            }
            if (isset($data['file'])) {
                unset($data['file']);
            }
            $goodmodel = new SystemGoods();
            $specmodel = new SystemGoodsSpec();
            //查找商家是否存
            $data['classid'] = $data['classid'];
            $insert = $data;
            unset($insert['spec']);
            if (empty($data['id'])) {
                Db::startTrans();
                try {
                    $data['id'] = $goodmodel->insertGetid($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            } else {
                Db::startTrans();
                try {
                    $goodmodel->where('id', $data['id'])->update($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $model = new SystemGoods();
            $specmodel = new SystemGoodsSpec();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            if ($results) {
                $results['images'] = json_decode($results['images'], true) ?? [];
                $results['speclist'] = $specmodel->order('is_default desc')->where(['isdel' => 1, 'goodid' => $results['id']])->select();
            }
            $platclass = (new PlatClass())->where('isshow', 1)->select();
            return view(null, [
                'row' => $results,
                'platclass' => $platclass
            ]);
        }
    }

    public function goodslist_del()
    {
        $id = trim(input('id'), ',');
        $model = new SystemGoods();
        $res = $model->where(['id' => $id])->update(['isdel' => 2]);
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function pgoodslist()
    {
        $model = new Pgoods();
        $data = $this->request->get();
        $results = $model->where('isdel', 1)->order('id desc');
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['isSale'])) {
            $results = $results->where('isSale', $data['isSale']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['images'] = json_decode($result['images'], true) ?? [];
            $result['goodspec'] = (new PgoodsSpec())->where(['isdel' => 1, 'goodid' => $result['id']])->order('is_default desc')->find();
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function pgoodslist_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'descs' => 'require',
                'images' => 'require',
                'spec' => 'require'
            ], [
                'title.require' => '服务名称不能为空',
                'descs.require' => '服务描述不能为空',
                'images.require' => '图片不能为空',
                'spec.require' => '规格必须填写'
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (empty($data['spec']) || count($data['spec']) <= 0) {
                return json(['code' => 1, 'data' => [], 'msg' => '规格信息不能少于一个']);
            }
            if (isset($data['file'])) {
                unset($data['file']);
            }
            $goodmodel = new Pgoods();
            $specmodel = new PgoodsSpec();
            //查找商家是否存
            $insert = $data;
            unset($insert['spec']);
            if (empty($data['id'])) {
                Db::startTrans();
                try {
                    $data['id'] = $goodmodel->insertGetid($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            } else {
                Db::startTrans();
                try {
                    $goodmodel->where('id', $data['id'])->update($insert);
                    foreach ($data['spec'] as $spec) {
                        if (!empty($spec['id'])) {
                            $specmodel->where(['goodid' => $data['id'], 'id' => $spec['id']])->update($spec);
                        } else {
                            unset($spec['id']);
                            $spec['goodid'] =  $data['id'];
                            $specmodel->insert($spec);
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                    return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
                }
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $model = new Pgoods();
            $specmodel = new PgoodsSpec();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            if ($results) {
                $results['images'] = json_decode($results['images'], true) ?? [];
                $results['speclist'] = $specmodel->order('is_default desc')->where(['isdel' => 1, 'goodid' => $results['id']])->select();
            }
            return view(null, [
                'row' => $results
            ]);
        }
    }

    public function pgoodslist_del()
    {
        $id = trim(input('id'), ',');
        $model = new Pgoods();
        $res = $model->where(['id' => $id])->update(['isdel' => 2]);
        if ($res) {
            return json(['code' => 0, 'status' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function withdraw()
    {
        $results = (new Withdraw())->order('id desc');
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['type_name'] = (new Business())->where('id', $result['user_id'])->value('title');
            if ($result['status'] == 1) {
                $result['status_text'] = '待审核';
            }
            if ($result['status'] == 2) {
                $result['status_text'] = '审核通过';
            }
            if ($result['status'] == 3) {
                $result['status_text'] = '已取消';
            }
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function withdraw_str()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require'
        ], [
            'id.require' => 'ID不能为空'
        ]);
        if (!$validate->check($data)) {
            return json(['status' => 1, 'msg' =>  $validate->getError()]);
        }
        $withdraw = (new Withdraw())->order('id desc')->where(['id' => $data['id'], 'status' => 1])->find();
        if (empty($withdraw)) {
            return json(['status' => 1, 'msg' =>  '记录不可操作']);
        }
        Db::startTrans();
        try {
            (new Business())->MoneyEdit($withdraw['user_id'], 2, $withdraw['money'], '商户提现平台驳回', 0);
            (new Withdraw())->where(['id' => $data['id'], 'status' => 1])->update(['status' => 3]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 1, 'code' => 1, 'msg' => '操作失败']);
        }
        return json(['status' => 0, 'code' => 0, 'msg' => '操作成功']);
    }

    public function withdraw_str2()
    {
        $data = $this->request->post();
        $validate = new Validate([
            'id' => 'require'
        ], [
            'id.require' => 'ID不能为空'
        ]);
        if (!$validate->check($data)) {
            return json(['status' => 1, 'msg' =>  $validate->getError()]);
        }
        $withdraw = (new Withdraw())->order('id desc')->where(['id' => $data['id'], 'status' => 1])->find();
        if (empty($withdraw)) {
            return json(['status' => 1, 'msg' =>  '记录不可操作']);
        }
        Db::startTrans();
        try {
            (new Withdraw())->where(['id' => $data['id'], 'status' => 1])->update(['status' => 2]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 1, 'code' => 1, 'msg' => '操作失败']);
        }
        return json(['status' => 0, 'code' => 0, 'msg' => '操作成功，请线下转账']);
    }

    public function PlatClass()
    {
        $list = (new PlatClass())->order(['sort' => 'desc', 'id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['name'])) {
            $list = $list->where('name', 'like', '%' . $data['name'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function platclass_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'images' => 'require'
            ], [
                'name.require' => '分类名称不能为空',
                'images.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new PlatClass())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new PlatClass())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new PlatClass())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function platclass_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $banner = (new PlatClass())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            $isshow = $banner['isshow'] == 1 ? 2 : 1;
            $res = (new PlatClass())->where('id', $id)->update(['isshow' => $isshow]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function banner()
    {
        $list = (new Banner())->order('id desc');
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['business'])) {
            $list = $list->where('business', $data['business']);
        }
        $list = $list->paginate(10)->each(function ($row) {
            $row['business_name'] = (new Business())->where(['id' => $row['business']])->value('title') ?? '';
            return $row;
        });
        $business = (new Business())->select();
        return view(null, [
            'list' => $list,
            'business' => $business
        ]);
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'url' => 'require'
            ], [
                'title.require' => '轮播图备注信息不能为空',
                'url.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new Banner())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Banner())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Banner())->where('id', $id)->find();
            $business = (new Business())->select();
            return view('', [
                'row' => $row,
                'business' => $business
            ]);
        }
    }

    public function banner_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $banner = (new Banner())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            $res = (new Banner())->where('id', $id)->delete();
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function config()
    {
        $list = (new Config())->paginate(10);
        foreach ($list as $lists) {
            if ($lists['name'] == 'rate') {
                $lists['value'] = json_decode($lists['value'], true);
            }
        }
        return view(null, [
            'list' => $list
        ]);
    }


    public function config_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'value' => 'require'
            ], [
                'name.require' => '名称不能为空',
                'value.require' => '路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if ($data['name'] == 'ptdatas' || $data['name'] == 'signtime') {
                $data['value'] = json_encode($data['value'], true);
            }
            if ($data['name'] == 'signaddress') {
                foreach ($data['value'] as $vms) {
                    if (empty($vms['address']) || empty($vms['latlng'])) {
                        return json(['status' => 1, 'msg' => '数据不能为空']);
                    }
                    $latlng =  explode(',', $vms['latlng']);
                    if (count($latlng) != 2) {
                        return json(['status' => 1, 'msg' => '坐标数据格式错误']);
                    }
                }
                $data['value'] = json_encode($data['value'], true);
            }
            if ($data['name'] == 'rate') {
                if (!isset($data['value']['allrate']) || !isset($data['value']['arearate']) || !isset($data['value']['cityrate']) || !isset($data['value']['streetrate'])) {
                    return json(['status' => 1, 'msg' => '参数设置错误']);
                }
                $data['value']['allrate'] = floor($data['value']['allrate'] * 100) / 100;
                $data['value']['arearate'] = floor($data['value']['arearate'] * 100) / 100;
                $data['value']['cityrate'] = floor($data['value']['cityrate'] * 100) / 100;
                if ($data['value']['allrate'] < ($data['value']['arearate'] + $data['value']['cityrate'] + $data['value']['streetrate'])) {
                    return json(['status' => 1, 'msg' => '请确保代理收益总和不超过商家抽成比例值']);
                }
                $data['value'] = json_encode($data['value'], true);
            }

            $res = (new Config())->allowfield(true)->save($data, ['name' => $data['name']]);
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('name', '');
            $row = (new Config())->where('name', $id)->find();
            if (!empty($row) && $row['name'] == 'rate') {
                $row['value'] = json_decode($row['value'], true);
            }
            if (!empty($row) && $row['name'] == 'ptdatas') {
                $row['value'] = json_decode($row['value'], true);
            }
            if (!empty($row) && $row['name'] == 'signtime') {
                $row['value'] = json_decode($row['value'], true);
            }
            if (!empty($row) && $row['name'] == 'signaddress') {
                $row['value'] = json_decode($row['value'], true) ?? [];
                $row['value'] = array_values($row['value']) ?? [];
            }
            return view('', [
                'row' => $row
            ]);
        }
    }
}
